﻿using Common;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ARPGDemo.Skill
{
	/// <summary>
    /// 技能释放器：附加到技能预制件上，用于定义所有释放器共有行为//技能管理器调用
    /// </summary>
	public abstract class SkillDeployer : MonoBehaviour {
        private SkillData currentSkillData;
        public SkillData CurrentSkillData
        {
            get { return currentSkillData; }
            set {
                currentSkillData = value;
                InitDeployer();//创建算法对象
            }
        }
        //选区算法对象
        private IAttackSelector selector;
        //影响算法对象
        private List<IImpact> impactList; 

        //初始化释放器:创建释放器依赖的算法对象
        private void InitDeployer()
        {
            //根据技能数据
            //currentSkillData.selectorType.ToString()-->Sector
            //所有实现类要求命名规范：命名空间.类名
            //ARPGDemo.Skill + 枚举 + AttackSelector
            selector = DeployerConfigFactory.CreatAttackSelector(CurrentSkillData);
            //Type typeAttackSelector = Type.GetType("ARPGDemo.Skill" + currentSkillData.selectorType.ToString() + "AttackSelector");
            //selector = Activator.CreateInstance(typeAttackSelector) as IAttackSelector;
            //currentSkillData.impactType
            //所有实现类要求命名规范：命名空间.类名
            //ARPGDemo.Skill + 影响名称 + Impact
            impactList = DeployerConfigFactory.CreatImpact(CurrentSkillData);
            //for (int i = 0; i < currentSkillData.impactType.Length; i++)
            //{
            //    Type typeImpact = Type.GetType("ARPGDemo.Skill" + currentSkillData.impactType[i].ToString() + "Impact");
            //    IImpact impact = Activator.CreateInstance(typeImpact) as IImpact;
            //    impactList.Add(impact);
            //}
            
        }
        /// <summary>
        /// 计算攻击目标
        /// </summary>
        public void CalculateTargets()
        {
            //根据选区计算目标
            currentSkillData.attackTargets = selector.SelectTarget(currentSkillData, transform);
            //测试,可以删除
            //foreach (var item in currentSkillData.attackTargets)
            //{
            //    print(item);
            //}
        }
        /// <summary>
        /// 回收技能
        /// </summary>
        public void CollectSkill()
        {
            GameObjectPool.Instance.CollectObject(gameObject, currentSkillData.durationTime);
        }
        /// <summary>
        /// 影响目标
        /// </summary>
        public void ImpactTarget()
        {
            //遍历影响算法对象
            foreach (var item in impactList)
            {
                item.DoImpact(this);
            }
        }
        /// <summary>
        /// 释放技能
        /// </summary>
        public abstract void DeployerSkill();
	}
}

